package util

import org.apache.spark.mllib.recommendation.Rating
import org.apache.spark.rdd.RDD

/**
  * Created by chenjianwen on 2016/3/7.
  */
object RDDOperation {
  //相加行为评分和订单评分
  def addActAndOrderScope(oneRdd:RDD[(Int,Rating)],orderRdd:RDD[(Int,Rating)]): RDD[(Int,Rating)] ={
    def transToKeyPro = (x:(Int,Rating))=>{
      val rating = x._2
      val timeRange = x._1
      ((rating.user,rating.product),(timeRange,rating))
    }

    val addRes = oneRdd.map(transToKeyPro).join(orderRdd.map(transToKeyPro)).map(x=>{
      x match{
        case ((userId,productId),((timeRange1,rating1),(timeRange2,rating2)))=>{
          (timeRange1,new Rating(userId,productId,rating1.rating+rating2.rating))
        }
      }

    })
    addRes
  }
}
